package com.yinp.proapp.module.study.recycler.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.yinp.proapp.adapter.ComViewHolder;
import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.base.activity.AppBaseActivity;
import com.yinp.proapp.databinding.ActivityMoreListTwoBinding;
import com.yinp.proapp.databinding.ItemMoreListTwoHorBinding;
import com.yinp.proapp.databinding.ItemVerMoreListBinding;
import com.yinp.proapp.module.study.recycler.bean.OneMoreListBean;
import com.yinp.proapp.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yinp
 * @Date: 2021/8/30
 * @Description:处理RecyclerView横向滑动冲突的卡顿
 */
public class MoreListTwoActivity extends AppBaseActivity<ActivityMoreListTwoBinding> {
    private CommonAdapter<OneMoreListBean> verticalAdapter;
    private List<OneMoreListBean> verticalList = new ArrayList<>();

    private List<String> horList = new ArrayList<>();

    @Override
    protected void initViews() {
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        initClick(bd.header.headerBackImg);
        initData();
        initRecycler();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == bd.header.headerBackImg) {
            finish();
        }
    }

    private void initData() {
        for (int i = 0; i < 20; i++) {
            verticalList.add(new OneMoreListBean(String.valueOf(i)));
            horList.add(String.valueOf(i));
        }
    }

    private void initRecycler() {
        verticalAdapter = new CommonAdapter<OneMoreListBean>(mContext,verticalList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                return new SingleViewHolder(ItemVerMoreListBinding.inflate(mInflater, parent, false));
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, OneMoreListBean item) {
                ItemVerMoreListBinding binding = (ItemVerMoreListBinding) holder.getBinding();
                binding.tvTitle.setText(item.getTitle());
                if (item.getHorAdapter() == null) {
                    item.setHorAdapter(new CommonAdapter<String>(mContext,horList) {
                        @Override
                        protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent2) {
                            return new SingleViewHolder(ItemMoreListTwoHorBinding.inflate(mInflater, parent2, false));
                        }

                        @Override
                        public void onBindItem(SingleViewHolder holder, int position, String item) {
                            ItemMoreListTwoHorBinding binding1 = (ItemMoreListTwoHorBinding) holder.getBinding();
                            binding1.tvTitle.setText(item);
                        }
                    });
                }
                binding.rvHor.setLayoutManager(new LinearLayoutManager(mContext));
                binding.rvHor.setAdapter(item.getHorAdapter());
            }
        };
        bd.rvVertical.setLayoutManager(new LinearLayoutManager(this));
        bd.rvVertical.setAdapter(verticalAdapter);
    }

    static class VerticalViewHolder extends ComViewHolder {
        ItemVerMoreListBinding binding;

        public VerticalViewHolder(ItemVerMoreListBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }

    static class HorViewHolder extends ComViewHolder {
        ItemMoreListTwoHorBinding binding;

        public HorViewHolder(ItemMoreListTwoHorBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }
}